package lion.dev.image;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;

import net.sf.image4j.codec.ico.ICODecoder;
import net.sf.image4j.codec.ico.ICOEncoder;

/**
 * @author helin
 * @email hl_0074@sina.com
 * @date 2016年11月3日
 */
public class ICOHandler implements IImageHandler {

	private BufferedImage image;

	@Override
	public void read(InputStream src) throws IOException {

		byte[] content = IOUtils.toByteArray(src);
		ByteArrayInputStream bais = new ByteArrayInputStream(content);
		try {
			this.image = ICODecoder.read(bais).get(0);
		} catch (Exception e) {
			bais.reset();
			this.image = ImageIO.read(bais);
		}
	}

	@Override
	public void read(BufferedImage image) {

		this.image = image;
	}

	@Override
	public IImageHandler compress(float quality, String type) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.compress(this.image, quality, type);
		return handler;
	}

	@Override
	public IImageHandler resize(int width, int height) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.sharpen(DefaultImageHandler.resize(this.image, width, height), 1);
		return handler;
	}

	@Override
	public IImageHandler resizeWidth(int width) {

		int height = (int) (this.image.getHeight() * (width * 1.0 / this.image.getWidth()));
		return this.resize(width, height);
	}

	@Override
	public IImageHandler resizeHeight(int height) {

		int width = (int) (this.image.getWidth() * (height * 1.0 / this.image.getHeight()));
		return this.resize(width, height);
	}

	@Override
	public IImageHandler resizeScale(float percent) {

		int width = (int) (this.image.getWidth() * percent);
		int height = (int) (this.image.getHeight() * percent);
		return this.resize(width, height);
	}

	@Override
	public IImageHandler rotate(Rotation rotation) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.rotate(this.image, rotation);
		return handler;
	}

	@Override
	public IImageHandler crop(int x, int y, int width, int height) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.crop(this.image, x, y, width, height);
		return handler;
	}

	@Override
	public void write(OutputStream os, String type) throws IOException {

		ICOEncoder.write(this.image, os);
	}

	@Override
	public IImageHandler watermark(BufferedImage watermark, Directoin directoin, float opacity) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.merge(this.image, watermark, directoin, opacity);
		return handler;
	}

	@Override
	public IImageHandler watermark(BufferedImage watermark, int x, int y, float opacity) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.merge(this.image, watermark, x, y, opacity);
		return handler;
	}

	@Override
	public IImageHandler sharpen(int times) {

		ICOHandler handler = new ICOHandler();
		handler.image = DefaultImageHandler.sharpen(this.image, times);
		return handler;
	}

	@Override
	public IImageHandler limit(int maxwidth, int maxheight) {

		int width = this.image.getWidth();
		int height = this.image.getHeight();
		if (maxwidth > 0 && width > maxwidth) {
			height = height * (maxwidth / width);
			width = maxwidth;
		}

		if (maxheight > 0 && height > maxheight) {
			width = width * height / maxheight;
			height = maxheight;
		}
		return this.resize(width, height);
	}

}
